package api.cie.rss.adapters;

import java.util.ArrayList;
import java.util.List;

import android.content.Context;
import android.content.Intent;
import android.database.Cursor;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.CheckBox;
import android.widget.TextView;
import api.cie.rss.ArticleDetailsActivity;
import api.cie.rss.R;
import api.cie.rss.common.Article;
import api.cie.rss.common.ParameterKeys;
import api.cie.rss.dal.DatabaseAdapter;
import api.cie.rss.dal.ParameterDAL;
import api.cie.rss.utils.Utils;

public class ArticleListAdapter extends BaseAdapter {

	private List<Article> mData = new ArrayList<Article>();
	private List<Article> selectedArticles;
	private LayoutInflater mInflater;
	final String articleStr = "article";
	
	public ArticleListAdapter(Context ctx) {
		super();
		selectedArticles = new ArrayList<Article>();
		mInflater = LayoutInflater.from(ctx);
	}
	
	public List<Article> getSelectedArticles() {
		return selectedArticles;
	}
	
	@Override
	public int getCount() {
		return mData.size();
	}

	@Override
	public Object getItem(int index) {
		return mData.get(index);
	}

	@Override
	public long getItemId(int position) {
		return position;
	}
	
	public void addItems(List<Article> aricles) {
		mData.clear();
		mData.addAll(aricles);
	}	

	@Override
	public View getView(int position, View view, ViewGroup group) {
		view = mInflater.inflate(R.layout.article_row, group, false);
		TextView tvTitle = (TextView)view.findViewById(R.id.textTitleArticle);		
		TextView tvSummary = (TextView)view.findViewById(R.id.textSummaryArticle);
		TextView tvDate = (TextView)view.findViewById(R.id.textDateArticle);
		
		final int pos = position;
		CheckBox chkArticle = (CheckBox)view.findViewById(R.id.checkArticle);
		chkArticle.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				if(((CheckBox)v).isChecked()) {
					selectedArticles.add(mData.get(pos));
				} else {
					selectedArticles.remove(mData.get(pos));
				}
			}
		});
		/*
		 * Change the font size according to settings parameters
		 */
		DatabaseAdapter dbAdapter = new DatabaseAdapter(view.getContext());
		dbAdapter.open();
		ParameterDAL parameterDAL = new ParameterDAL(dbAdapter);
		Cursor cursor = parameterDAL.fetchParameterByKey(ParameterKeys.FONT_SIZE);
		if (cursor != null) {
			String fontSize = cursor.getString(cursor.getColumnIndex(ParameterDAL.KEY_PARAMVALUE));
			if (fontSize.equals(ParameterKeys.FONT_LARGE)) {
				tvTitle.setTextSize(ParameterKeys.TITLE_LARGE);
				tvSummary.setTextSize(ParameterKeys.CHARACTER_LARGE);
				tvDate.setTextSize(ParameterKeys.CHARACTER_LARGE);
			} else if (fontSize.equals(ParameterKeys.FONT_NORMAL)) {
				tvTitle.setTextSize(ParameterKeys.TITLE_NORMAL);
				tvSummary.setTextSize(ParameterKeys.CHARACTER_NORMAL);
				tvDate.setTextSize(ParameterKeys.CHARACTER_NORMAL);
			} else if (fontSize.equals(ParameterKeys.FONT_SMALL)) {
				tvTitle.setTextSize(ParameterKeys.TITLE_SMALL);
				tvSummary.setTextSize(ParameterKeys.CHARACTER_SMALL);
				tvDate.setTextSize(ParameterKeys.CHARACTER_SMALL);
			}
		}
		dbAdapter.close();		
		
		final Article currentArticle = mData.get(position);
		if (currentArticle != null) {
			tvTitle.setText(currentArticle.getTitle());
			tvSummary.setText(Utils.formatSummary(currentArticle.getDescription()));
			tvDate.setText(Utils.dateToString(currentArticle.getPublishingDate()));
		}
		
		tvTitle.setOnClickListener(new OnClickListener() {			
			@Override
			public void onClick(View v) {
				Intent intent = new Intent(v.getContext(), ArticleDetailsActivity.class);
				
				// Add the parameters into bundle to transmit 
				Bundle bundle = new Bundle();	
				ArrayList<Article> articles = (ArrayList<Article>)mData;
				bundle.putSerializable(v.getContext().getString(R.string.article), articles);
				bundle.putInt(v.getContext().getString(R.string.position), pos);
				intent.putExtras(bundle);
				v.getContext().startActivity(intent);
			}
		});
		return view;
	}

}
